package com.auth.config.token;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.SpringSecurityCoreVersion;

import java.io.Serializable;

/**
 *  自定义存储
 * @author: hw
 * @date: 2021/11/19 14:59
 */
public class CustomAuthenticationToken extends AbstractAuthenticationToken implements Serializable {

    private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;

    private  String auth_type;
    /**
     *  用户信息全部放在这里面，如用户名，手机号，密码等
     */
    private final Object principal;
    /**
     * 这里保存的证书信息，如密码，验证码等
     */
    private Object credentials;

    private String appId;

    private String redirectUrl;

    public String getAppId() {
        return appId;
    }

    public String getRedirectUrl() {
        return redirectUrl;
    }

    /**
     * 构造未认证之前用户信息
     * @param auth_type
     * @param principal
     * @param credentials
     */
    public CustomAuthenticationToken(String appId, String redirectUrl, String auth_type, Object principal, Object credentials) {
        super(null);
        this.appId = appId;
        this.redirectUrl = redirectUrl;
        this.auth_type = auth_type;
        this.principal = principal;
        this.credentials = credentials;
        this.setAuthenticated(false);
    }


    public String getAuth_type() {
        return auth_type;
    }

    @Override
    public Object getPrincipal() {
        return principal;
    }

    @Override
    public Object getCredentials() {
        return credentials;
    }

    @Override
    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException("Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        }
        super.setAuthenticated(false);
    }

    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
        credentials = null;
    }
}
